<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/12/04/0004
 * Time: 14:55
 */

namespace app\sports_admin\model;


use think\Model;
use traits\model\SoftDelete;

class Parents extends Model
{
    use SoftDelete;
    protected $autoWriteTimestamp = true;

    protected $table = 'parents';

    protected $pk = 'id';

    protected $hidden = ['password'];

    public function childrens()
    {
        return $this->hasMany('student','parent_id','id');
    }
    public function teacher()
    {
        return $this->belongsTo('teacher','teacher_id','id');
    }
    /**
     * 教师列表
     * @param $page 第几页
     * @param $rows 每页显示的条数
     * @param $sort 要排序的字段
     * @param $sortOrder 升序/降序
     * @param $searchName 要检索的字段
     */
    public static function getParentsList($page,$rows,$sort,$sortOrder,$searchName)
    {
        $result = self::where(function($query)use($searchName){
            if($searchName){
                $query->where('name|phone','like',"%{$searchName}%");
            }
        })->order("{$sort}","{$sortOrder}")->page($page)->limit($rows)->select();
        $count = self::where(function($query)use($searchName){
            if($searchName){
                $query->where('name|phone','like',"%{$searchName}%");
            }
        })->count();
        return json(['rows'=>$result,'total'=>$count]);
    }

}